<?php
/**
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/4/11
 * Time: 11:33
 */
class BingFangChaXunModel extends BaseModel
{
    /**
     * 方法名：getBingfangXunchaSql()
     * 功    能: 获取标本数据
     * @param $user_department_id
     * @param $user_department
     * @param $jsonData
     * @return $async_result
     */
    public function getBingfangXunchaSql($user_department_id,$user_department,$jsonData,$modify_time = '')
    {
        $yiyuan_id = $this->yiyuan_id;
        if(Yaconf::get("config$yiyuan_id.fenbiao_state") == 'true')//分表机制开启
        {
            $data_bingfang_xuncha = "data_bingfang_xuncha".$user_department_id;
            $data_bingfang_xuncha_histroy = "data_bingfang_xuncha_histroy".$user_department_id;
        }
        else
        {
            $data_bingfang_xuncha = "data_bingfang_xuncha";
            $data_bingfang_xuncha_histroy = "data_bingfang_xuncha_histroy";
        }
       // $data = json_decode($jsonData,true);//将json格式数据$jsonData转换成php变量，并返回数组
        date_default_timezone_set("Asia/Shanghai");
        $today = date("Y-m-d");
        $today_time = date("Y-m-d H:i:s");

        //$chaxun = "a.id = b.zhixing_id and  a.bingqu = '$user_department' and date(b.modify_time) = '$today'";
        $arr1 = DB::select('a.*,count(b.zhixing_id) AS count')
            ->from(array($data_bingfang_xuncha,"a"))
            ->join(array($data_bingfang_xuncha_histroy,"b"),"left")
            ->on("a.id","=","b.zhixing_id")
            ->where("a.bingqu","=",$user_department)
            ->and_where("date(b.modify_time)","=",$today)
            ->and_where("a.yiyuan_id","=",$yiyuan_id)
            ->group_by("zhixing_id")
            ->execute()
            ->as_array();
        //$arr1 = M($data_bingfang_xuncha .' a',$data_bingfang_xuncha_histroy .' b,')->where($chaxun)->field('a.*,count(b.zhixing_id) AS count')->group('zhixing_id')->select();
        $arr2 = DB::select("*")
            ->from($data_bingfang_xuncha)
            ->where("bingqu","=",$user_department)
            ->and_where("yiyuan_id","=",$yiyuan_id)
            ->execute()
            ->as_array();
        //$arr2 = M($data_bingfang_xuncha)->where("bingqu = '$user_department'")->select();
        if(!empty($arr1)){
            $arr = array_merge($arr1,$arr2);
        }else{
            $arr = $arr2;
        }

        $item=array();
        foreach($arr as $k=>$v){
            if(!isset($item[$v['id']])){
                $item[$v['id']]=$v;
            }else{
                $item[$v['id']]['count']+=$v['count'];
            }
        }
        //print_r($arr);exit;
        //echo M()->getLastSql();exit;
        $bingfang_xuncha_list = $item;
        $tiaoshu = 200;
        $async_result = array();
        $number = ceil(count($bingfang_xuncha_list)/$tiaoshu);
        for($i = 0; $i < $number; $i++)
        {
            $value = $i * $tiaoshu;
            $xuncha_content = array_slice($bingfang_xuncha_list,$value,$tiaoshu);//在$bingfang_xuncha_list$value+1条数据开始取$tiaoshu条数据
            foreach($xuncha_content as $key => $one_xuncha)
            {
                if($key == 0)//第一条数据
                {
                    $async_result[$i] .= "REPLACE INTO data_bingfang_xuncha (id,bingqu,bingfanghao,state,beizhu,qrcode,count,modify_time) SELECT  '".$one_xuncha["id"]."','".$one_xuncha["bingqu"]."','".$one_xuncha["bingfanghao"]."','".$one_xuncha["state"]."','".$one_xuncha["beizhu"]."','".$one_xuncha["qrcode"]."','".$one_xuncha["count"]."','".$today_time."'";
                }
                else
                {
                    $async_result[$i] .= " union all SELECT '".$one_xuncha["id"]."','".$one_xuncha["bingqu"]."','".$one_xuncha["bingfanghao"]."','".$one_xuncha["state"]."','".$one_xuncha["beizhu"]."','".$one_xuncha["qrcode"]."','".$one_xuncha["count"]."','".$today_time."'";
                }
            }
        }
        return $async_result;
    }


    public function addBingFangXunChaHis($biao_name,$data)
    {
        $affect_rows = DB::insert($biao_name, array_keys($data))->values(array_values($data))->execute();
        return $affect_rows;
    }

}